package com.foogui.boot.listener;

import com.alibaba.fastjson.JSON;
import com.foogui.boot.constant.RocketMqConstant;
import com.foogui.boot.vo.RocketMqMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SOURCE_TOPIC,
        consumerGroup = RocketMqConstant.SOURCE_GROUP,
        selectorExpression = RocketMqConstant.SOURCE_TAG
        // 指定消费者线程数，默认64，生产中请注意配置，避免过大或者过小
        //consumeThreadMax = 5
        //是否有序消费
        //consumeMode = ConsumeMode.ORDERLY
)
public class ConsumerListener implements RocketMQListener<RocketMqMessage> {
    @Override
    public void onMessage(RocketMqMessage message) {
        log.info("收到的消息是" + JSON.toJSONString(message));
        try {
            // 方法执行完成之后会自动进行进行ack
            TimeUnit.SECONDS.sleep(3);
            // 出现异常，将会自动进入重试队列
            // int ex = 10 / 0;
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }
        log.info("休眠了3s后消费完成");

    }
}
